package com.axjy.config.etc;

import com.axjy.pojo.User;
import com.axjy.service.UserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

public class SmbmsRealm extends AuthorizingRealm {

    private UserService userService;

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String userCode= (String) token.getPrincipal();
        User loginUser=userService.searchUserByUserCode(userCode);
        if(loginUser==null){
            return null;
        }
        AuthenticationInfo info= new SimpleAuthenticationInfo(loginUser,loginUser.getPassword(), ByteSource.Util.bytes(loginUser.getSalt()),super.getName());
        return info;
    }
}
